package com.paradogs.gs.modules.inventory.framework;

import com.gitee.sunchenbin.mybatis.actable.utils.ClassScaner;
import com.paradogs.common.utils.R;
import com.paradogs.gs.misc.annotations.GameManager;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author: yumi
 * @date: 2024/10/29  16:50
 * @Description: TODO
 */
@Slf4j
@Component
public class ItemGainProxyLoader implements ApplicationRunner {

    public static final Map<ItemType, Method> ANNOTATION_MAPPING = new LinkedHashMap<>();


    @Override
    public void run(ApplicationArguments args) throws Exception {
        Set<Class> classes = ClassScaner.scan("com.paradogs", GameManager.class);

        for (Class clz : classes) {
            for (Method method : clz.getDeclaredMethods()) {
                ItemGainProxy itemGainProxy = AnnotationUtils.getAnnotation(method, ItemGainProxy.class);
                if (itemGainProxy != null) {
                    if (ANNOTATION_MAPPING.containsKey(itemGainProxy.value())) {
                        Method duplicate = ANNOTATION_MAPPING.get(itemGainProxy.value());
                        throw new RuntimeException(String.format(
                                "重复注册 @ItemGainProxy: %s.%s, %s.%s"
                                , duplicate.getDeclaringClass().getName()
                                , duplicate.getName()
                                , method.getDeclaringClass().getName()
                                , method.getName()
                        ));
                    }
                    ANNOTATION_MAPPING.put(itemGainProxy.value(), method);
                }
            }

        }

    }
}
